﻿#include "ZControlGroupBox.h"
#include <QDebug>
#include <QFont>
ZControlGroupBox::ZControlGroupBox(QWidget *parent)
    :QGroupBox(parent)
{
    initContent();
    initConnection();
    initSpeedLevels();
}

void ZControlGroupBox::initSpeedLevels()
{
    QList<double> levels;
    levels << 0.003;
    levels << 0.01;
    levels << 0.03;
    levels << 0.1;
    levels << 0.3;
    levels << 1;
    levels << 3;
    levels << 10;
    levels << 30;

    setupSpeedLevels(levels);
}


void ZControlGroupBox::initContent()
{

    verticalLayout = new QVBoxLayout;
    verticalLayout->setSpacing(1);
    verticalSliderSpeedZ = new QSlider(this);

    verticalSliderSpeedZ->setMinimumSize(QSize(20, 0));
    verticalSliderSpeedZ->setMaximum(9);
    verticalSliderSpeedZ->setPageStep(1);
    verticalSliderSpeedZ->setOrientation(Qt::Vertical);
    verticalLayout->addWidget(verticalSliderSpeedZ);

    labelSpeedZ = new QLabel(this);
    verticalLayout->addWidget(labelSpeedZ);

    horizontalLayout = new QHBoxLayout(this);
    horizontalLayout->setSpacing(1);

    m_zControl = new ZControlWidget(this);
    horizontalLayout->addWidget(m_zControl);
    horizontalLayout->addLayout(verticalLayout);

    pushButtonAutoFocus = new QPushButton(this);
    pushButtonAutoFocus->setText("Auto\nFocus");
//    pushButtonAutoFocus->setMinimumSize(QSize(40, 40));
//    pushButtonAutoFocus->setMaximumSize(QSize(40, 40));
    horizontalLayout->addWidget(pushButtonAutoFocus);

    setLayout(horizontalLayout);

    setTitle("Z Move Control");
//    QFont font;
//    font.setPointSize(12);
//    setFont(font);
}

void ZControlGroupBox::initConnection()
{
    connect(m_zControl, SIGNAL(moveUp()), this, SLOT(on_moveUp()));
    connect(m_zControl, SIGNAL(moveDown()), this, SLOT(on_moveDown()));
    connect(m_zControl, SIGNAL(moveUpStop()), this, SIGNAL(moveUpStop()));
    connect(m_zControl, SIGNAL(moveDownStop()), this, SIGNAL(moveDownStop()));
    connect(m_zControl, SIGNAL(stop()), this, SIGNAL(stop()));

    connect(verticalSliderSpeedZ, SIGNAL(valueChanged(int)), this, SLOT(on_sliderValueChanged(int)));

    connect(pushButtonAutoFocus, SIGNAL(clicked()), this, SIGNAL(autoFocus()));
}

void ZControlGroupBox::on_moveUp()
{
    double spd = speed();
    emit moveUp(spd);
}

void ZControlGroupBox::on_moveDown()
{
    double spd = speed();
    emit moveDown(spd);
}

void ZControlGroupBox::setupSpeedLevels(QList<double> levels)
{
    if(levels.isEmpty())
    {
        return;
    }
    m_levels = levels;
    int num = m_levels.size();
    verticalSliderSpeedZ->setMaximum(num - 1);
    verticalSliderSpeedZ->setValue(num / 2);
}

void ZControlGroupBox::speedUp()
{
    int max = m_levels.size() - 1;
    int v = verticalSliderSpeedZ->value();
    int v_new = qBound(0, v + 1, max);
    if(v != v_new)
    {
        verticalSliderSpeedZ->setValue(v_new);
    }
}

void ZControlGroupBox::speedDown()
{
    int max = m_levels.size() - 1;
    int v = verticalSliderSpeedZ->value();
    int v_new = qBound(0, v - 1, max);
    if(v != v_new)
    {
        verticalSliderSpeedZ->setValue(v_new);
    }
}

void ZControlGroupBox::on_sliderValueChanged(int n)
{
    double speed = m_levels.at(n);
    labelSpeedZ->setText(QString("%1 \n  mm/s").arg(speed, 6, 'f', 3, ' '));
    qDebug() << "ZControlGroupBox changeSpeed = " << speed;
    emit changeSpeed(speed);
}

double ZControlGroupBox::speed()
{
    int v = verticalSliderSpeedZ->value();
    double spd = m_levels.at(v);
    return spd;
}
